package com.tuling.mall.user.controller;

import com.tuling.mall.common.R;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/httpClient")
public class HttpClientController {

    @RequestMapping("/findOrder/{userId}")
    public R findOrder(@PathVariable("userId") String userId) {
        // 创建 HttpClient 客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建 HttpPost 请求
        HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/sendSms");
        // 设置长连接
        httpPost.setHeader("Connection", "keep-alive");
        // 设置代理（模拟浏览器版本）
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
        // Content-Type: application/json;charset=utf-8
        httpPost.setHeader("Content-Type","application/json;charset=utf-8");
        // Accept: application/json, text/plain, */*
        httpPost.setHeader("Accept","application/json, text/plain, */*");
        // 设置 Cookie
//        httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

        // 创建 HttpPost 参数
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("ID", "z1");
//        jsonObject.put("NAME", "主任医生");
//        jsonObject.put("DOMAIN", "his");
        CloseableHttpResponse httpResponse = null;
        try {
            // 设置 HttpPost 参数
//            StringEntity stringEntity = new StringEntity(JSON.toJSONString(jsonObject),"UTF-8");
//            stringEntity.setContentEncoding("UTF-8");

//            httpPost.setEntity(stringEntity);

            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // 输出请求结果
            System.out.println(EntityUtils.toString(httpEntity));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        // 无论如何必须关闭连接
        finally {
            try {
                if (httpResponse != null) {
                    httpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.success();
    }
}
